<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/black/pace-theme-flat-top.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous" defer></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"tryhub.top","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.23.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.json","localsearch":{"enable":true,"top_n_per_article":1,"unescape":true,"preload":true,"trigger":"auto"}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="应用场景第三方调用的我的接口,上传若干个文件,我用 MultiPartFile[] 数组接收,之后我调用其他服务的接口,把文件发送过去,统一保存 目前存在的问题就是,当你使用 feign 传递 MultipartFile 对象时,接收方无法解析,所以需要重写 feign 的 Encoder ,让它支持 MultipartFile 类型以及 MultipartFile[] 数组类型">
<meta property="og:type" content="article">
<meta property="og:title" content="springcloud feign 多文件上传">
<meta property="og:url" content="https://tryhub.top/2019/06/05/springcloud-feign-%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/index.html">
<meta property="og:site_name" content="奇姆的博客">
<meta property="og:description" content="应用场景第三方调用的我的接口,上传若干个文件,我用 MultiPartFile[] 数组接收,之后我调用其他服务的接口,把文件发送过去,统一保存 目前存在的问题就是,当你使用 feign 传递 MultipartFile 对象时,接收方无法解析,所以需要重写 feign 的 Encoder ,让它支持 MultipartFile 类型以及 MultipartFile[] 数组类型">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-06-05T09:02:58.000Z">
<meta property="article:modified_time" content="2023-07-07T07:27:00.943Z">
<meta property="article:author" content="Jaron Chim">
<meta property="article:tag" content="springcloud">
<meta property="article:tag" content="feign">
<meta property="article:tag" content="MultiPartFile">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://tryhub.top/2019/06/05/springcloud-feign-%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://tryhub.top/2019/06/05/springcloud-feign-%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/","path":"2019/06/05/springcloud-feign-多文件上传/","title":"springcloud feign 多文件上传"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>springcloud feign 多文件上传 | 奇姆的博客</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js" defer></script>
  <script async src="https://hm.baidu.com/hm.js?1ad13b7aff8bfe9206fa7e12d5c4a4f0"></script>







  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script><script src="/js/bookmark.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>





  <script src="/js/third-party/pace.js" defer></script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="奇姆的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">奇姆的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="nav-number">1.</span> <span class="nav-text">应用场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E8%BE%93%E5%87%BA"><span class="nav-number">2.</span> <span class="nav-text">异常输出</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Maven%E5%BC%95%E5%85%A5jar%E5%8C%85"><span class="nav-number">3.</span> <span class="nav-text">Maven引入jar包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SpringMultipartEncoder-%E7%B1%BB-%E9%87%8D%E5%86%99-FormEncoder-%E4%BD%BF%E5%BE%97-feign-%E5%8F%AF%E4%BB%A5%E6%94%AF%E6%8C%81%E6%96%87%E4%BB%B6%E4%BC%A0%E8%BE%93"><span class="nav-number">4.</span> <span class="nav-text">SpringMultipartEncoder 类 - 重写 FormEncoder 使得 feign 可以支持文件传输</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FeignMultipartSupportConfig-%E7%B1%BB-%E9%85%8D%E7%BD%AE-Encoder-%E6%B3%A8%E5%85%A5-messageConverters"><span class="nav-number">5.</span> <span class="nav-text">FeignMultipartSupportConfig 类 - 配置 Encoder 注入 messageConverters</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B6%88%E8%B4%B9%E8%80%85%E7%9A%84%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%AE%A2%E6%88%B7%E7%AB%AF-FileClient-%E7%B1%BB-FeignClient"><span class="nav-number">6.</span> <span class="nav-text">消费者的文件服务客户端 FileClient 类 - FeignClient</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8A%E9%9D%A2%E6%9C%89%E4%B8%AA-SpringMultipartEncoder-%E7%B1%BB-%E8%BF%99%E4%B8%AA%E6%98%AF%E6%88%91%E4%BB%AC%E9%87%8D%E5%86%99%E5%88%9A%E6%89%8D%E5%AF%BC%E5%85%A5jar%E5%8C%85%E4%B8%AD%E7%9A%84-FormEncoder-%E4%BB%A5%E6%94%AF%E6%8C%81%E4%BB%BB%E6%84%8F%E6%95%B0%E9%87%8F%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0-%E5%90%8C%E6%97%B6%E8%BF%99%E9%87%8C%E7%94%A8-RequestPart-%E6%B3%A8%E6%84%8F%E4%B8%8D%E8%83%BD%E7%94%A8-RequestParam-%E5%A6%82%E6%9E%9C%E7%94%A8-RequestParam-%E8%A2%AB%E8%B0%83%E7%94%A8%E7%9A%84%E6%9C%8D%E5%8A%A1%E6%96%B9%E4%BC%9A%E5%87%BA%E7%8E%B0%E5%A6%82%E4%B8%8B%E9%94%99%E8%AF%AF"><span class="nav-number">6.1.</span> <span class="nav-text">上面有个 SpringMultipartEncoder 类,这个是我们重写刚才导入jar包中的 FormEncoder ,以支持任意数量文件上传,同时这里用 @RequestPart ,注意不能用 @RequestParam,如果用 @RequestParam ,被调用的服务方会出现如下错误</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#RequestParam-%E9%80%82%E7%94%A8%E4%BA%8E-name-valueString-%E7%B1%BB%E5%9E%8B%E7%9A%84%E8%AF%B7%E6%B1%82%E5%9F%9F"><span class="nav-number">6.1.1.</span> <span class="nav-text">@RequestParam 适用于 name-valueString 类型的请求域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#RequestPart-%E9%80%82%E7%94%A8%E4%BA%8E%E5%A4%8D%E6%9D%82%E7%9A%84%E8%AF%B7%E6%B1%82%E5%9F%9F-JSON-XML"><span class="nav-number">6.1.2.</span> <span class="nav-text">@RequestPart 适用于复杂的请求域 (JSON,XML)</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B6%88%E8%B4%B9%E8%80%85%E7%9A%84%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E6%8E%A7%E5%88%B6%E5%B1%82"><span class="nav-number">7.</span> <span class="nav-text">消费者的文件服务控制层</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%9F%E4%BA%A7%E8%80%85%E7%9A%84%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E6%8E%A7%E5%88%B6%E5%B1%82"><span class="nav-number">8.</span> <span class="nav-text">生产者的文件服务控制层</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jaron Chim"
      src="/images/jaron.gif">
  <p class="site-author-name" itemprop="name">Jaron Chim</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">76</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">95</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">144</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/admin-Jaron" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;admin-Jaron" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:tryhub@tryhub.top" title="E-Mail → mailto:tryhub@tryhub.top" rel="noopener me"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tryhub.top/2019/06/05/springcloud-feign-%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/jaron.gif">
      <meta itemprop="name" content="Jaron Chim">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇姆的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="springcloud feign 多文件上传 | 奇姆的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          springcloud feign 多文件上传
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-06-05 17:02:58" itemprop="dateCreated datePublished" datetime="2019-06-05T17:02:58+08:00">2019-06-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-07-07 15:27:00" itemprop="dateModified" datetime="2023-07-07T15:27:00+08:00">2023-07-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/springcloud/" itemprop="url" rel="index"><span itemprop="name">springcloud</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/springcloud/feign/" itemprop="url" rel="index"><span itemprop="name">feign</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/springcloud/feign/MultiPartFile/" itemprop="url" rel="index"><span itemprop="name">MultiPartFile</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="应用场景"><a href="#应用场景" class="headerlink" title="应用场景"></a>应用场景</h2><p>第三方调用的我的接口,上传若干个文件,我用 <code>MultiPartFile[]</code> 数组接收,之后我调用其他服务的接口,把文件发送过去,统一保存</p>
<p>目前存在的问题就是,当你使用 <code>feign</code> 传递 <code>MultipartFile</code> 对象时,接收方无法解析,所以需要重写 <code>feign</code> 的 <code>Encoder</code> ,让它支持 <code>MultipartFile</code> 类型以及 <code>MultipartFile[]</code> 数组类型</p>
<span id="more"></span>

<h2 id="异常输出"><a href="#异常输出" class="headerlink" title="异常输出"></a>异常输出</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[0]-&gt;org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[&quot;inputStream&quot;]-&gt;java.io.FileInputStream[&quot;fd&quot;])</span><br></pre></td></tr></table></figure>

<h2 id="Maven引入jar包"><a href="#Maven引入jar包" class="headerlink" title="Maven引入jar包"></a>Maven引入jar包</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 支持feign传递MultipartFile --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.github.openfeign.form<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>feign-form<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.github.openfeign.form<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>feign-form-spring<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="SpringMultipartEncoder-类-重写-FormEncoder-使得-feign-可以支持文件传输"><a href="#SpringMultipartEncoder-类-重写-FormEncoder-使得-feign-可以支持文件传输" class="headerlink" title="SpringMultipartEncoder 类 - 重写 FormEncoder 使得 feign 可以支持文件传输"></a><code>SpringMultipartEncoder</code> 类 - 重写 <code>FormEncoder</code> 使得 <code>feign</code> 可以支持文件传输</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> feign.RequestTemplate;</span><br><span class="line"><span class="keyword">import</span> feign.codec.EncodeException;</span><br><span class="line"><span class="keyword">import</span> feign.codec.Encoder;</span><br><span class="line"><span class="keyword">import</span> feign.form.ContentType;</span><br><span class="line"><span class="keyword">import</span> feign.form.FormEncoder;</span><br><span class="line"><span class="keyword">import</span> feign.form.MultipartFormContentProcessor;</span><br><span class="line"><span class="keyword">import</span> feign.form.spring.SpringManyMultipartFilesWriter;</span><br><span class="line"><span class="keyword">import</span> feign.form.spring.SpringSingleMultipartFileWriter;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.multipart.MultipartFile;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Type;</span><br><span class="line"><span class="keyword">import</span> java.util.Collections;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 支持feign传递MultipartFile</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SpringMultipartEncoder</span> <span class="keyword">extends</span> <span class="title class_">FormEncoder</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Constructor with the default Feign&#x27;s encoder as a delegate.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">SpringMultipartEncoder</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>(<span class="keyword">new</span> <span class="title class_">Default</span>());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Constructor with specified delegate encoder.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> delegate delegate encoder, if this encoder couldn&#x27;t encode object.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">SpringMultipartEncoder</span><span class="params">(Encoder delegate)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(delegate);</span><br><span class="line"></span><br><span class="line">        <span class="type">MultipartFormContentProcessor</span> <span class="variable">processor</span> <span class="operator">=</span> (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);</span><br><span class="line">        processor.addWriter(<span class="keyword">new</span> <span class="title class_">SpringSingleMultipartFileWriter</span>());</span><br><span class="line">        processor.addWriter(<span class="keyword">new</span> <span class="title class_">SpringManyMultipartFilesWriter</span>());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">encode</span><span class="params">(Object object, Type bodyType, RequestTemplate template)</span> <span class="keyword">throws</span> EncodeException &#123;</span><br><span class="line">        <span class="comment">// 单MultipartFile判断</span></span><br><span class="line">        <span class="keyword">if</span> (bodyType.equals(MultipartFile.class)) &#123;</span><br><span class="line">            <span class="type">MultipartFile</span> <span class="variable">file</span> <span class="operator">=</span> (MultipartFile) object;</span><br><span class="line">            <span class="type">Map</span> <span class="variable">data</span> <span class="operator">=</span> Collections.singletonMap(file.getName(), object);</span><br><span class="line">            <span class="built_in">super</span>.encode(data, MAP_STRING_WILDCARD, template);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (bodyType.equals(MultipartFile[].class)) &#123;</span><br><span class="line">            <span class="comment">// MultipartFile数组处理</span></span><br><span class="line">            MultipartFile[] file = (MultipartFile[]) object;</span><br><span class="line">            <span class="keyword">if</span> (file != <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="type">Map</span> <span class="variable">data</span> <span class="operator">=</span> Collections.singletonMap(file.length == <span class="number">0</span> ? <span class="string">&quot;&quot;</span> : file[<span class="number">0</span>].getName(), object);</span><br><span class="line">                <span class="built_in">super</span>.encode(data, MAP_STRING_WILDCARD, template);</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 其他类型调用父类默认处理方法</span></span><br><span class="line">        <span class="built_in">super</span>.encode(object, bodyType, template);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="FeignMultipartSupportConfig-类-配置-Encoder-注入-messageConverters"><a href="#FeignMultipartSupportConfig-类-配置-Encoder-注入-messageConverters" class="headerlink" title="FeignMultipartSupportConfig 类 - 配置 Encoder 注入 messageConverters"></a><code>FeignMultipartSupportConfig</code> 类 - 配置 <code>Encoder</code> 注入 <code>messageConverters</code></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> feign.codec.Encoder;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.ObjectFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.http.HttpMessageConverters;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cloud.openfeign.support.SpringEncoder;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FeignMultipartSupportConfig</span> &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//    @Bean</span></span><br><span class="line"><span class="comment">//    public Encoder feignEncoder(ObjectFactory&lt;HttpMessageConverters&gt; messageConverters) &#123;</span></span><br><span class="line"><span class="comment">//        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ObjectFactory&lt;HttpMessageConverters&gt; messageConverters;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> Encoder <span class="title function_">feignEncoder</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">SpringMultipartEncoder</span>(<span class="keyword">new</span> <span class="title class_">SpringEncoder</span>(messageConverters));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="消费者的文件服务客户端-FileClient-类-FeignClient"><a href="#消费者的文件服务客户端-FileClient-类-FeignClient" class="headerlink" title="消费者的文件服务客户端 FileClient 类 - FeignClient"></a>消费者的文件服务客户端 <code>FileClient</code> 类 - FeignClient</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 文件存储客户端</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FeignClient(name = &quot;file-service&quot;, configuration = SpringMultipartEncoder.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">FileClient</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 上传文件存储对象</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> files</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> firstKey</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@PostMapping(value = &quot;/api/file/upload&quot;, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)</span></span><br><span class="line">    List&lt;String&gt; <span class="title function_">upload</span><span class="params">(<span class="meta">@RequestPart(value = &quot;files&quot;)</span> MultipartFile[] files, <span class="meta">@RequestParam(&quot;firstKey&quot;)</span> String firstKey)</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="上面有个-SpringMultipartEncoder-类-这个是我们重写刚才导入jar包中的-FormEncoder-以支持任意数量文件上传-同时这里用-RequestPart-注意不能用-RequestParam-如果用-RequestParam-被调用的服务方会出现如下错误"><a href="#上面有个-SpringMultipartEncoder-类-这个是我们重写刚才导入jar包中的-FormEncoder-以支持任意数量文件上传-同时这里用-RequestPart-注意不能用-RequestParam-如果用-RequestParam-被调用的服务方会出现如下错误" class="headerlink" title="上面有个 SpringMultipartEncoder 类,这个是我们重写刚才导入jar包中的 FormEncoder ,以支持任意数量文件上传,同时这里用 @RequestPart ,注意不能用 @RequestParam,如果用 @RequestParam ,被调用的服务方会出现如下错误"></a>上面有个 <code>SpringMultipartEncoder</code> 类,这个是我们重写刚才导入jar包中的 <code>FormEncoder</code> ,以支持任意数量文件上传,同时这里用 <em><code>@RequestPart</code></em> ,注意不能用 <em><code>@RequestParam</code></em>,如果用 <em><code>@RequestParam</code></em> ,被调用的服务方会出现如下错误</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Caused by: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found</span><br></pre></td></tr></table></figure>
<ul>
<li><h4 id="RequestParam-适用于-name-valueString-类型的请求域"><a href="#RequestParam-适用于-name-valueString-类型的请求域" class="headerlink" title="@RequestParam 适用于 name-valueString 类型的请求域"></a><code>@RequestParam</code> 适用于 <code>name-valueString</code> 类型的请求域</h4></li>
<li><h4 id="RequestPart-适用于复杂的请求域-JSON-XML"><a href="#RequestPart-适用于复杂的请求域-JSON-XML" class="headerlink" title="@RequestPart 适用于复杂的请求域 (JSON,XML)"></a><code>@RequestPart</code> 适用于复杂的请求域 (JSON,XML)</h4></li>
</ul>
<h2 id="消费者的文件服务控制层"><a href="#消费者的文件服务控制层" class="headerlink" title="消费者的文件服务控制层"></a>消费者的文件服务控制层</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> FileClient fileClient;  <span class="comment">// 消费者的文件服务客户端</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 上传文件存储对象</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> files</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> firstKey</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@PostMapping(&quot;file/v1/upload&quot;)</span></span><br><span class="line"><span class="keyword">public</span> List&lt;String&gt; <span class="title function_">fileUpload</span><span class="params">(<span class="meta">@RequestParam(&quot;files&quot;)</span> MultipartFile[] files, String firstKey)</span> &#123;</span><br><span class="line">    List&lt;String&gt; strings = fileClient.upload(files, firstKey);</span><br><span class="line">    <span class="keyword">return</span> strings;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="生产者的文件服务控制层"><a href="#生产者的文件服务控制层" class="headerlink" title="生产者的文件服务控制层"></a>生产者的文件服务控制层</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 上传文件存储对象</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> files</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> firstKey</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@PostMapping(&quot;/api/file/upload&quot;)</span></span><br><span class="line"><span class="keyword">public</span> List&lt;String&gt; <span class="title function_">upload</span><span class="params">(<span class="meta">@RequestParam(&quot;files&quot;)</span> MultipartFile[] files, <span class="meta">@RequestParam(&quot;firstKey&quot;)</span> String firstKey)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">LinkedList</span>&lt;&gt;();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.jpg" alt="Jaron Chim 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.jpg" alt="Jaron Chim 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/springcloud/" rel="tag"># springcloud</a>
              <a href="/tags/feign/" rel="tag"># feign</a>
              <a href="/tags/MultiPartFile/" rel="tag"># MultiPartFile</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2019/05/20/Java%E7%9A%84Excel%E8%AF%BB%E5%86%99-easyExcel%E6%96%B9%E6%B3%95%E5%B0%81%E8%A3%85/" rel="prev" title="Java的Excel读写-easyExcel方法封装">
                  <i class="fa fa-angle-left"></i> Java的Excel读写-easyExcel方法封装
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2019/09/18/HttpUtils-%E9%98%BF%E9%87%8C%E4%BA%91%E5%B8%82%E5%9C%BA-api%E7%BD%91%E5%85%B3%E8%AF%B7%E6%B1%82%E7%AD%BE%E5%90%8D/" rel="next" title="HttpUtils 阿里云市场 api网关请求签名">
                  HttpUtils 阿里云市场 api网关请求签名 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">粤ICP备2025405034号-1 </a>
  </div>
  <div class="copyright">
    &copy; 2016 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-user"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jaron Chim</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
